﻿//==============================================================================  
//Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
//GUID：f9189d66-c2cd-4c10-b1ab-e95778570bae
//CLR Version: 4.0.30319.18033
//Code Author：Kevin Wang
//Contact：Email(Admin@9un.org),QQ(265382 or 74344)
//Filename：LibVlcMediaPlayerInterop
//Namespace：RuanRang.Media.Core.Internal.Interop
//Functions：LibVlcMediaPlayerInterop  
//Created by Kevin Wang at 2013/3/3 15:44:04 http://blog.9un.org
//============================================================================== 
using System;
using System.Runtime.InteropServices;
using System.Security;
using RuanRang.Media.Core.Internal.DataStructures;
using log4net;

namespace RuanRang.Media.Core.Internal.Interop
{
    internal class LibVlcMediaPlayerInterop : BaseInterop
    {
        /// <summary>
        /// Initializes static members of the <see cref="LibVlcMediaPlayerInterop"/> class.
        /// </summary>
        static LibVlcMediaPlayerInterop()
        {
            Logger = LogManager.GetLogger(typeof(LibVlcMediaPlayerInterop));
        }

        #region Mediaplayer

        /// <summary>
        /// Libvlc_media_player_new_unmanageds the specified libvlc_instance.
        /// </summary>
        /// <param name="libvlc_instance">The libvlc_instance.</param>
        /// <returns>IntPtr.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_new")]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_player_new_unmanaged(IntPtr libvlc_instance);

        /// <summary>
        /// Create an empty Media Player object
        /// </summary>
        /// <param name="libvlc_instance">Pointer to instance</param>
        /// <returns>Media player descriptor</returns>
        public static IntPtr libvlc_media_player_new(IntPtr libvlc_instance)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_new");
            }
            return libvlc_media_player_new_unmanaged(libvlc_instance);
        }

        /// <summary>
        /// Libvlc_media_player_new_from_media_unmanageds the specified libvlc_media.
        /// </summary>
        /// <param name="libvlc_media">The libvlc_media.</param>
        /// <returns>IntPtr.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_new_from_media")]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_player_new_from_media_unmanaged(IntPtr libvlc_media);

        /// <summary>
        /// Create a Media Player object from a Media
        /// </summary>
        /// <param name="libvlc_media">Media descriptor</param>
        /// <returns>Media player descriptor</returns>
        public static IntPtr libvlc_media_player_new_from_media(IntPtr libvlc_media)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_new_from_media");
            }
            return libvlc_media_player_new_from_media_unmanaged(libvlc_media);
        }

        /// <summary>
        /// Libvlc_media_player_get_media_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>IntPtr.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_get_media")]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_player_get_media_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Get the media used by the media_player
        /// </summary>
        /// <param name="libvlc_mediaplayer">Mediaplayer descriptor</param>
        /// <returns>Media descriptor</returns>
        public static IntPtr libvlc_media_player_get_media(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_get_media");
            }
            return libvlc_media_player_get_media_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_set_media_unmanageds the specified libvlc_media_player_t.
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <param name="libvlc_media_t">The libvlc_media_t.</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_media")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_media_unmanaged(IntPtr libvlc_media_player_t, IntPtr libvlc_media_t);

        /// <summary>
        /// Libvlc_media_player_set_medias the specified libvlc_media_player_t.
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <param name="libvlc_media_t">The libvlc_media_t.</param>
        public static void libvlc_media_player_set_media(IntPtr libvlc_media_player_t, IntPtr libvlc_media_t)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_media");
            }
            libvlc_media_player_set_media_unmanaged(libvlc_media_player_t, libvlc_media_t);
        }

        /// <summary>
        /// Libvlc_media_player_play_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>System.Int32.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_play")]
        [SuppressUnmanagedCodeSecurity]
        private static extern int libvlc_media_player_play_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Play
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>System.Int32.</returns>
        public static int libvlc_media_player_play(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_play");
            }
            return libvlc_media_player_play_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_pause_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_pause")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_pause_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Pause
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        public static void libvlc_media_player_pause(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_pause");
            }
            libvlc_media_player_pause_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_stop_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        [DllImport("libvlc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, EntryPoint = "libvlc_media_player_stop")]
        //[SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_stop_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Stop
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        public static void libvlc_media_player_stop(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_stop");
            }
            libvlc_media_player_stop_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_is_playing_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns></returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_is_playing", CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        private static extern int libvlc_media_player_is_playing_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Libvlc_media_player_is_playings the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns></returns>
        public static int libvlc_media_player_is_playing(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_stop");
            }
            return libvlc_media_player_is_playing_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Set the drawable where the media player should render its video output
        /// </summary>
        /// <param name="libvlc_mediaplayer">The Media Player</param>
        /// <param name="libvlc_drawable">The libvlc_drawable_t where the media player should render its video</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_hwnd")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_hwnd_unmanaged(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable);

        /// <summary>
        /// Libvlc_media_player_set_hwnds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="libvlc_drawable">The libvlc_drawable.</param>
        public static void libvlc_media_player_set_hwnd(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_hwnd");
            }
            libvlc_media_player_set_hwnd_unmanaged(libvlc_mediaplayer, libvlc_drawable);
        }

        /// <summary>
        /// Set the drawable where the media player should render its video output
        /// </summary>
        /// <param name="libvlc_mediaplayer">The Media Player</param>
        /// <param name="libvlc_drawable">The libvlc_drawable_t where the media player should render its video</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_xwindow")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_xwindow_unmanaged(IntPtr libvlc_mediaplayer, Int32 libvlc_drawable);

        /// <summary>
        /// Libvlc_media_player_set_xwindows the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="libvlc_drawable">The libvlc_drawable.</param>
        public static void libvlc_media_player_set_xwindow(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_xwindow");
            }
            libvlc_media_player_set_xwindow_unmanaged(libvlc_mediaplayer, libvlc_drawable.ToInt32());
        }

        /// <summary>
        /// Set the drawable where the media player should render its video output
        /// </summary>
        /// <param name="libvlc_mediaplayer">The Media Player</param>
        /// <param name="libvlc_drawable">The libvlc_drawable_t where the media player should render its video</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_agl")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_agl_unmanaged(IntPtr libvlc_mediaplayer, Int32 libvlc_drawable);

        /// <summary>
        /// Libvlc_media_player_set_agls the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="libvlc_drawable">The libvlc_drawable.</param>
        public static void libvlc_media_player_set_agl(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_agl");
            }
            libvlc_media_player_set_agl_unmanaged(libvlc_mediaplayer, libvlc_drawable.ToInt32());
        }

        /// <summary>
        /// Set the drawable where the media player should render its video output
        /// </summary>
        /// <param name="libvlc_mediaplayer">The Media Player</param>
        /// <param name="libvlc_drawable">The libvlc_drawable_t where the media player should render its video</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_nsobject")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_nsobject_unmanaged(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable);

        /// <summary>
        /// Libvlc_media_player_set_nsobjects the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="libvlc_drawable">The libvlc_drawable.</param>
        public static void libvlc_media_player_set_nsobject(IntPtr libvlc_mediaplayer, IntPtr libvlc_drawable)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_nsobject");
            }
            libvlc_media_player_set_nsobject_unmanaged(libvlc_mediaplayer, libvlc_drawable);
        }

        /// <summary>
        /// Libvlc_media_player_get_length_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>Int64.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_get_length")]
        [SuppressUnmanagedCodeSecurity]
        private static extern Int64 libvlc_media_player_get_length_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Get the current movie length (in ms)
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>Int64.</returns>
        public static Int64 libvlc_media_player_get_length(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_get_length");
            }
            return libvlc_media_player_get_length_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_get_time_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>Int64.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_get_time")]
        [SuppressUnmanagedCodeSecurity]
        private static extern Int64 libvlc_media_player_get_time_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Get the current movie time (in ms)
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>Int64.</returns>
        public static Int64 libvlc_media_player_get_time(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_get_time");
            }
            return libvlc_media_player_get_time_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_set_time_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="time">The time.</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_time")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_time_unmanaged(IntPtr libvlc_mediaplayer, Int64 time);

        /// <summary>
        /// Set the movie time (in ms)
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="time">The time.</param>
        public static void libvlc_media_player_set_time(IntPtr libvlc_mediaplayer, Int64 time)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_time");
            }
            libvlc_media_player_set_time_unmanaged(libvlc_mediaplayer, time);
        }

        /// <summary>
        /// Libvlc_media_player_get_position_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>System.Single.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_get_position")]
        [SuppressUnmanagedCodeSecurity]
        private static extern float libvlc_media_player_get_position_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Get current position (float part)
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <returns>System.Single.</returns>
        public static float libvlc_media_player_get_position(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_get_position");
            }
            return libvlc_media_player_get_position_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>s
        /// Libvlc_media_player_set_position_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="position">The position.</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_set_position")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_set_position_unmanaged(IntPtr libvlc_mediaplayer, float position);

        /// <summary>
        /// Set current position (float part)
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        /// <param name="position">The position.</param>
        public static void libvlc_media_player_set_position(IntPtr libvlc_mediaplayer, float position)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_set_position");
            }
            libvlc_media_player_set_position_unmanaged(libvlc_mediaplayer, position);
        }

        /// <summary>
        /// Libvlc_media_player_release_unmanageds the specified libvlc_mediaplayer.
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_release")]
        [SuppressUnmanagedCodeSecurity]
        private static extern void libvlc_media_player_release_unmanaged(IntPtr libvlc_mediaplayer);

        /// <summary>
        /// Release a media_player after use Decrement the reference count of a media player object
        /// </summary>
        /// <param name="libvlc_mediaplayer">The libvlc_mediaplayer.</param>
        public static void libvlc_media_player_release(IntPtr libvlc_mediaplayer)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_release");
            }
            libvlc_media_player_release_unmanaged(libvlc_mediaplayer);
        }

        /// <summary>
        /// Libvlc_media_player_event_manager_unmanageds the specified libvlc_media_player_t.
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <returns>IntPtr.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_event_manager")]
        [SuppressUnmanagedCodeSecurity]
        private static extern IntPtr libvlc_media_player_event_manager_unmanaged(IntPtr libvlc_media_player_t);

        /// <summary>
        /// Get the Event MediaLibraryFactory from which the media player send event
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <returns>IntPtr.</returns>
        public static IntPtr libvlc_media_player_event_manager(IntPtr libvlc_media_player_t)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_event_manager");
            }
            return libvlc_media_player_event_manager_unmanaged(libvlc_media_player_t);
        }

        /// <summary>
        /// Libvlc_media_player_get_fps_unmanageds the specified libvlc_media_player_t.
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <returns>System.Single.</returns>
        [DllImport("libvlc", ExactSpelling = true, EntryPoint = "libvlc_media_player_get_fps")]
        [SuppressUnmanagedCodeSecurity]
        private static extern float libvlc_media_player_get_fps_unmanaged(IntPtr libvlc_media_player_t);

        /// <summary>
        /// Libvlc_media_player_get_fpses the specified libvlc_media_player_t.
        /// </summary>
        /// <param name="libvlc_media_player_t">The libvlc_media_player_t.</param>
        /// <returns>System.Single.</returns>
        public static float libvlc_media_player_get_fps(IntPtr libvlc_media_player_t)
        {
            if (CallTraceEnabled)
            {
                TraceCall("libvlc_media_player_get_fps");
            }
            return libvlc_media_player_get_fps_unmanaged(libvlc_media_player_t);
        }

        #endregion

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_media_player_next_frame(IntPtr playerInstance);

        // libvlc_media_player_navigate

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_track_description_release(IntPtr ptrack);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern uint libvlc_media_player_has_vout(IntPtr playerInstance);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern int libvlc_media_player_is_seekable(IntPtr playerInstance);


        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_media_player_set_chapter(IntPtr playerInstance, int chapter);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern int libvlc_media_player_get_chapter(IntPtr playerInstance);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern int libvlc_media_player_get_chapter_count(IntPtr playerInstance);

        // libvlc_media_player_will_play

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern int libvlc_media_player_get_chapter_count_for_title(IntPtr playerInstance, int title);

        /// <summary>
        /// Set movie title
        /// </summary>
        /// <param name="playerInstance">The Media Player</param>
        /// <param name="title">Title number to play</param>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_media_player_set_title(IntPtr playerInstance, int title);

        /// <summary>
        /// Get movie title
        /// </summary>
        /// <param name="playerInstance">The Media Player</param>
        /// <returns>Title number currently playing, or -1</returns>
        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern int libvlc_media_player_get_title(IntPtr playerInstance);

        // libvlc_media_player_get_title_count

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_media_player_previous_chapter(IntPtr playerInstance);

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern void libvlc_media_player_next_chapter(IntPtr playerInstance);

        // libvlc_media_player_get_rate
        // libvlc_media_player_set_rate

        [DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
        public static extern libvlc_state_t libvlc_media_player_get_state(IntPtr playerInstance);
    }
}
